package com.cn.wanxi.service.impl;

import com.cn.wanxi.entity.UserEntity;
import com.cn.wanxi.mapper.UserMapper;
import com.cn.wanxi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserEntity> findAll(UserEntity userEntity) {
////        if(userEntity.getPageEntity().getCurPage()==null||userEntity.getPageEntity().getPageSize()==null){
//        System.out.println(userEntity);
//            userEntity.setPageEntity(new PageEntity(1,10));
////        }
//        System.out.println(userEntity);
        return userMapper.findAll(userEntity);
    }

    @Override
    public List<UserEntity> findAll() {
        return null;
    }

    @Override
    public String login(UserEntity userEntity) {
        String result;
        if (!userEntity.getCode().equals(userEntity.getSessionCode())) {
            result = "error_code";
        }else {
            UserEntity model=userMapper.findByUsernameAndPassword(userEntity);
            if (model!=null){
                result = "success";
            }else {
                result = "error";
            }
        }
        return result;
    }

    @Override
    public String edit(UserEntity userEntity) {
        UserEntity entity = userMapper.findByUsernameForEdit(userEntity);
        if (entity != null) {
            return "isHave";
        } else {
            int result = userMapper.edit(userEntity);
            return result == 1 ? "success" : "error";
        }
    }

//    @Override
//    public String editpassword(UserEntity userEntity) {
//        UserEntity entity=userMapper.findByUsernameForEditpassword(userEntity);
//        if (entity != null){
//            return "isHave";
//        }else {
//            int result = userMapper.editpassword(userEntity);
//            return result == 1 ? "success" : "error";
//        }
//    }

    @Override
    public String editpassword(UserEntity userEntity) {
        UserEntity entity=userMapper.findByUsernameForEditpassword(userEntity);
        if (entity != null){
            return "isHave";
        }else {
            int result = userMapper.editpassword(userEntity);
            return result == 1 ? "success" : "error";
        }
    }

    @Override
    public int editpassword(Integer id) {
        return userMapper.editpassword(id);
    }

    @Override
    public String add(UserEntity userEntity) {
        UserEntity entity = userMapper.findByUsername(userEntity.getUsername());
        if (entity != null){
            return "isHave";
        }else {
            int result = userMapper.add(userEntity);
            return result == 1 ? "success" : "error";
        }
    }

    @Override
    public int getCount(UserEntity userEntity) {

        return userMapper.getCount(userEntity);
    }

    @Override
    public UserEntity findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public int del(Integer id) {
        return userMapper.del(id);
    }

    @Override
    public UserEntity findById(Integer id) {

        return userMapper.findById(id);
    }
}

